﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Common.Models;
using Module.Core.BusinessLogic;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;

namespace Web.Cms.Advertistment
{
    public partial class AdvertismentManager : AdminUserControl
    {
        private CHRTList<AdvertisementPositionsInfo> listAdvPostion;
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            try
            {
                listAdvPostion = AdvertisementPositionsBL.SelectAll();
                CHRTList<AdvertisementInfo> listInfo = AdvertisementBL.SelectAll();
                if (listInfo != null && listInfo.Count > 0)
                {
                    grdMedias.DataSource = listInfo;
                    grdMedias.DataBind();
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void grdMedias_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int advId = ConvertUtility.ToInt32(e.CommandArgument);
            try
            {
                switch (e.CommandName)
                {
                    case "deletemedia":
                        AdvertisementBL.Delete(advId, 0);
                        break;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        protected void grdMedias_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                AdvertisementInfo advertisementInfo = (AdvertisementInfo)e.Row.DataItem;
                Label lblSTT = (Label)e.Row.FindControl("lblSTT");
                lblSTT.Text = (e.Row.DataItemIndex + 1).ToString();
                DropDownList dropAdvertises = (DropDownList)e.Row.FindControl("dropAdvertises");
                dropAdvertises.DataSource = listAdvPostion;
                dropAdvertises.DataTextField = "Title";
                dropAdvertises.DataValueField = "PositionID";
                dropAdvertises.DataBind();
                dropAdvertises.SelectedValue = advertisementInfo.PositionID.ToString();

                DropDownList dropOrder = (DropDownList)e.Row.FindControl("dropOrder");
                ObjMiscUtility.FillIndex(dropOrder, 1, 100, 0);
                dropOrder.SelectedValue = advertisementInfo.Priority.ToString();

                CheckBox cboIsEnable = (CheckBox)e.Row.FindControl("cboIsEnable");
                Label lblActionStatus = (Label)e.Row.FindControl("lblActionStatus");
                if (advertisementInfo.IsEnable == 1)
                {
                    lblActionStatus.Text = "<b><font color='blue'>Đang hoạt động</font></b>";
                    cboIsEnable.Checked = true;
                }
                if (advertisementInfo.IsEnable == 0)
                {
                    lblActionStatus.Text = "<b><font color='red'>Ngừng hoạt động</font></b>";
                    cboIsEnable.Checked = false;
                }
                if (advertisementInfo.EndDate < DateTime.Now)
                {
                    lblActionStatus.Text = "<b><font color='red'>Ngừng hoạt động</font></b>";
                    cboIsEnable.Enabled = false;
                }

                Label lblStartDate = (Label)e.Row.FindControl("lblStartDate");
                lblStartDate.Text = MiscUtility.GetLongDateTime(advertisementInfo.StartDate);
                Label lblEndDate = (Label)e.Row.FindControl("lblEndDate");
                lblEndDate.Text = MiscUtility.GetLongDateTime(advertisementInfo.EndDate);
                Literal litMedias = (Literal)e.Row.FindControl("litMedia");
                if (advertisementInfo.MediaType == (int)AdvertisementType.Image)
                {
                    litMedias.Text = ObjMiscUtility.strInitImage(advertisementInfo.MediaSrc, 100, 0); ;
                }
                if (advertisementInfo.MediaType == (int)AdvertisementType.Flash)
                {
                    litMedias.Text = ObjMiscUtility.strInitFlash(advertisementInfo.MediaSrc, 100, 50);
                }
                if (advertisementInfo.MediaType == (int)AdvertisementType.Movie)
                {
                    litMedias.Text = ObjMiscUtility.strInitMovie(advertisementInfo.MediaSrc, 100, 50);
                }
            }
        }
        protected void grdMedias_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdMedias.PageIndex = e.NewPageIndex;
        }
    }
}